package com.espoir.test;


import com.espoir.pojo.Shp;
import com.espoir.service.IShpService;
import com.espoir.shp.read01.App2;
import com.espoir.shp.read01.ShapeModel;
import com.espoir.shp.read02.App;
import com.espoir.shp.read02.Point;
import com.espoir.util.JSONUtils;
import com.espoir.util.WKTUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ShpApp222 {

    @Autowired
    IShpService shpService;

    @Test
    public void testFindAll() {
        Shp shp = new Shp();
        shp.setName("shp测试");
        ArrayList<Point> points = new ArrayList<>();
        points.add(new Point(1,2));
        points.add(new Point(3,2));
        points.add(new Point(5,2));
        String pointsStr = JSONUtils.PointsToString(points);
        shp.setPoint(pointsStr);
        shpService.save(shp);
    }

    /**
     * 读取数据库数据保存到 shapefile
     */
    @Test
    public void testDBTOShp() {
        List<Shp> list = shpService.findAll();
        List<Point> points = new ArrayList<>();
        list.forEach((item)->{
            Point point = WKTUtils.wktPointToPoint(item.getPoint());
            points.add(point);
        });
        App.writeShp(points,"D:\\resource\\sjdata\\test\\test2");
    }

    @Test
    public void testDBTOShp2() {
        List<Shp> list = shpService.findAll();
        List<Point> points = new ArrayList<>();
        list.forEach((item)->{
            Point point = WKTUtils.wktPointToPoint(item.getPoint());
            points.add(point);
        });
        App.writeShp1(points,"D:\\resource\\sjdata\\test\\test555");
    }

    @Test
    public void testShpToDB(){
        String filePath = "D:\\resource\\sjdata\\test\\test2.shp";
        List<ShapeModel> list = App2.readShp(filePath);
        list.forEach((item)->{
            //String wktPoint = WKTUtils.PointToWKTPoint(item);

            Shp shp = new Shp();
            shp.setName("shpPoint");
            //shp.setPoint(wktPoint);
            shpService.save2(shp);
        });
    }

    @Test
    public void testReadShpFile(){
        String filePath = "D:\\resource\\sjdata\\test\\test.shp";
//        String filePath = "D:\\resource\\sjdata\\sj.shp";

        //List<Point> list = App2.readShp(filePath);
        //System.out.println("list = " + list);
//        list.forEach((item)->{
//            String wktPoint = WKTUtils.PointToWKTPoint(item);
//
//            Shp shp = new Shp();
//            shp.setName("shp测试");
//            shp.setPoint(wktPoint);
//            shpService.save2(shp);
//        });
    }


    /**
     * 根据 id 查询数据
     */
    @Test
    public void testFindById2() {
        Shp shp = shpService.findById2(30);
        String pointStr = shp.getPoint();
        Point p = WKTUtils.wktPointToPoint(pointStr);
        System.out.println("p = " + p);
        System.out.println("shp = " + shp);
    }

    @Test
    public void testSave2(){
        String point = "POINT(123 123)";
        Shp shp = new Shp();
        shp.setName("shp测试");
//        ArrayList<Point> points = new ArrayList<>();
//        points.add(new Point(1,2));
//        points.add(new Point(3,2));
//        points.add(new Point(5,2));
//        String pointsStr = JSONUtils.PointsToString(points);
        shp.setPoint(point);
        shpService.save2(shp);
        shpService.save2(shp);
        shpService.save2(shp);
        shpService.save2(shp);
    }


}
